package com.huifer.wgx.controller;

import com.huifer.wgx.model.FileEntity;
import com.huifer.wgx.pojo.UploadFileResponse;
import com.huifer.wgx.service.FileStorageInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

/**
 * <p>Title : FileController </p>
 * <p>Description : fileController</p>
 *
 * @author huifer
 * @date 2019-03-20
 */
@RestController
@RequestMapping("file")
@Api(tags = "FileController", description = "文件管理")
public class FileController {


    @Autowired
    private FileStorageInterface fileStorageInterface;

    @ApiOperation("上传一个文件")
    @PostMapping("/upload_file")
    public UploadFileResponse uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("work_id") Long workId,
            @RequestParam("personnel_id") Long personnelId
    ) {
        FileEntity dbFile = fileStorageInterface.storeFile(file, workId, personnelId);

        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/file/download_file/")
                .path(dbFile.getFileId().toString())
                .toUriString();

        return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
                file.getContentType(), file.getSize());
    }

    @ApiOperation("上传一组文件")
    @PostMapping("/upload_multiple_files")
    public List<UploadFileResponse> uploadMultipleFiles(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam("work_id") Long workId,
            @RequestParam("personnel_id") Long personnelId

    ) {
        return Arrays.asList(files)
                .stream()
                .map(file -> uploadFile(file, workId, personnelId))
                .collect(Collectors.toList());
    }

    @ApiOperation("下载一个文件")
    @GetMapping("/download_file/{fileId}")
    public ResponseEntity<Resource> downloadFile(@PathVariable Long fileId) {
        FileEntity dbFile = fileStorageInterface.getFile(fileId);

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(dbFile.getFileType()))
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=\"" + dbFile.getFileName() + "\"")
                .body(new ByteArrayResource(dbFile.getFileData()));
    }

}
